package com.gobo.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gobo.eduservice.clients.UcenterMemberClient;
import com.gobo.eduservice.pojo.EduComment;
import com.gobo.eduservice.mapper.EduCommentMapper;
import com.gobo.eduservice.service.EduCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gobo.servicebase.vo.MemberInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author gobo-generator
 * @since 2021-09-25
 */
@Service
public class EduCommentServiceImpl extends ServiceImpl<EduCommentMapper, EduComment> implements EduCommentService {

    @Autowired
    private UcenterMemberClient memberClientclient;

    @Override
    public Map<String, Object> getFrontPageByCourseId(Page<EduComment> page,String courseId) {

        QueryWrapper<EduComment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("course_id",courseId);
        baseMapper.selectPage(page,queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("items", page.getRecords());
        map.put("current", page.getCurrent());
        map.put("pages", page.getPages());
        map.put("size", page.getSize());
        map.put("total", page.getTotal());
        map.put("hasNext", page.hasNext());
        map.put("hasPrevious", page.hasPrevious());
        return map;
    }

    @Override
    public boolean saveEduCommentContainsMemberInfo(EduComment comment,String memberId) {
        //插入用户信息
        MemberInfoVo memberInfoVo = memberClientclient.getMemberInfoVo(memberId);
        comment.setAvatar(memberInfoVo.getAvatar());
        comment.setNickname(memberInfoVo.getNickname());
        comment.setMemberId(memberInfoVo.getId());
        int i = baseMapper.insert(comment);
        return i==1;
    }
}
